﻿using System;
using System.Timers;

namespace HobSoft.SoccerBet2014.Domain.Common.Schedule
{
    public class ServiceTimer : Domain.Schedule.ITimer, IDisposable
    {
        private readonly Timer timer = new Timer();
        public ServiceTimer()
        {
            timer.Elapsed += Timer_Elapsed;
        }

        protected void RaiseTick()
        {
            if (Tick != null)
                Tick(this, EventArgs.Empty);
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            RaiseTick();
        }

        public double Interval
        {
            get
            {
                return timer.Interval;
            }
            set
            {
                timer.Interval = value;
            }
        }

        public object Tag
        {
            get;
            set;
        }

        public void Start()
        {
            timer.Start();
        }

        public void Stop()
        {
            timer.Stop();
        }

        public event EventHandler Tick;

        public void Dispose()
        {
            if (timer != null)
            {
                timer.Stop();
                timer.Dispose();
            }
        }
    }
}
